# t_pp_pr_lr.R
# Outputs: Tables containing distributions of populist let and right parties presented in appendix
# Dependencies: master_elec.dta

setwd('')

library(haven)
library(tidyverse)
library(xtable)

df <- read_dta('master_elec.dta')

# Populist Participation Left, Right or Both
df$pp_lr <- 'None'
df$pp_lr[which(df$populistparty_leftonly == 1)] <- 'Populist Participation (Left)'
df$pp_lr[which(df$populistparty_rightonly == 1)] <- 'Populist Participation (Right)'
df$pp_lr[which(df$populistparty_both == 1)] <- 'Populist Participation (Both)'

# Populist Participation Left, Right or Both
df$pr_lr <- 'None'
df$pr_lr[which(df$l_populistpresence_leftonly == 1)] <- 'Populist Representation (Left)'
df$pr_lr[which(df$l_populistpresence_rightonly == 1)] <- 'Populist Representation (Right)'
df$pr_lr[which(df$l_populistpresence_both == 1)] <- 'Populist Representation (Both)'

# Full sample ------------------------------------------------------------------

df %>% group_by(pp_lr) %>%
    summarize(N = n()) %>%
    rename(` ` = pp_lr) %>%
    xtable() %>%
    print(include.rownames = F, booktabs = T, floating = F,
          file = 'tables/t_pp_lr.tex')


df %>% group_by(pr_lr) %>%
    summarize(N = n()) %>%
    rename(` ` = pr_lr) %>%
    xtable() %>%
    print(include.rownames = F, booktabs = T, floating = F,
          file = 'tables/t_pr_lr.tex')

# Eastern Europe ---------------------------------------------------------------

df_e <- df %>% filter(poco == 2)

df_e %>% group_by(pp_lr) %>%
  summarize(N = n()) %>%
  rename(` ` = pp_lr) %>%
  xtable() %>%
  print(include.rownames = F, booktabs = T, floating = F,
        file = 'tables/t_pp_lr_east.tex')

df_e %>% group_by(pr_lr) %>%
  summarize(N = n()) %>%
  rename(` ` = pr_lr) %>%
  xtable() %>%
  print(include.rownames = F, booktabs = T, floating = F,
        file = 'tables/t_pr_lr_east.tex')

# Western Europe ---------------------------------------------------------------

df_w <- df %>% filter(poco == 1)

df_w %>% group_by(pp_lr) %>%
  summarize(N = n()) %>%
  rename(` ` = pp_lr) %>%
  xtable() %>%
  print(include.rownames = F, booktabs = T, floating = F,
        file = 'tables/t_pp_lr_west.tex')

df_w %>% group_by(pr_lr) %>%
  summarize(N = n()) %>%
  rename(` ` = pr_lr) %>%
  xtable() %>%
  print(include.rownames = F, booktabs = T, floating = F,
        file = 'tables/t_pr_lr_west.tex')